package com.uzhie.spring;

import org.apache.log4j.Logger;

import javax.servlet.*;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;

/**
 * Created by TangFei on 2018/8/15.
 * httpRequest 包装过滤器
 */
public class HttpWrapperFilter implements Filter {

    private Logger logger = Logger.getLogger(getClass());

    @Override
    public void init(FilterConfig filterConfig) throws ServletException {
        logger.info("load HttpWrapperFilter enter method init");
    }

    @Override
    public void doFilter(ServletRequest servletRequest, ServletResponse servletResponse, FilterChain filterChain) throws IOException, ServletException {
        logger.info("enter HttpCrossFilter method doFilter");
        if (servletRequest instanceof HttpServletRequest && servletResponse instanceof HttpServletResponse) {
            HttpServletRequestWrapper requestWrapper = new HttpServletRequestWrapper((HttpServletRequest) servletRequest);
            filterChain.doFilter(requestWrapper, servletResponse);
        } else {
            logger.error("ServletRequest && ServletResponse error");
        }
    }

    @Override
    public void destroy() {
        logger.info("enter HttpWrapperFilter method destroy");
    }
}
